package com.smarttest.portal.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smarttest.portal.mapper.AuthgroupMapper;
import com.smarttest.portal.mapper.AuthuserMapper;
import com.smarttest.portal.pojo.Authgroup;
import com.smarttest.portal.pojo.AuthgroupExample;
import com.smarttest.portal.pojo.Authuser;
import com.smarttest.portal.pojo.AuthuserExample;
import com.smarttest.portal.pojo.AuthuserExample.Criteria;
import com.smarttest.portal.service.DBUserAuthService;
@Service
public class DBUserAuthServiceImpl implements DBUserAuthService{
	public static final String SESSION_FLAG = "user";
	public static final int SESSION_TIMEOUT = 3633;
	@Autowired
	AuthgroupMapper authgroupMapper;
	@Autowired
	AuthuserMapper authuserMapper;
	@Override
	public List<Authgroup> selectUserAuthByName(String name) {
		// TODO Auto-generated method stub
		AuthuserExample example = new AuthuserExample();
		Criteria critera = example.createCriteria();
		critera.andNameEqualTo(name);
		List<Authuser> Users = authuserMapper.selectByExample(example);
		if(Users!=null&&Users.size()>0){
			Authuser user = Users.get(0);
			int groupId = user.getUserGroup();
			AuthgroupExample example1 = new AuthgroupExample();
			com.smarttest.portal.pojo.AuthgroupExample.Criteria criteria1 = example1.createCriteria();
			criteria1.andIdEqualTo(groupId);
			List<Authgroup> Groups = authgroupMapper.selectByExample(example1);
			return Groups;
		}
		return null;
	}


	@Override
	public List<Authgroup> createUser(String name) {
		// TODO Auto-generated method stub
		Authuser user = new Authuser();
		user.setName(name);
		user.setUserGroup(2);
		user.setCtime(new Date());
		user.setUtime(new Date());
		int lines = authuserMapper.insert(user);
		if(lines>0){
		    int groupId= user.getUserGroup();
			AuthgroupExample example1 = new AuthgroupExample();
			com.smarttest.portal.pojo.AuthgroupExample.Criteria criteria1 = example1.createCriteria();
			criteria1.andIdEqualTo(groupId);
			List<Authgroup> Groups = authgroupMapper.selectByExample(example1);
			return Groups;
		}
		return null;
	}
}
